package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/table")
    public TableData findAllCustomer(Customer customer,Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<Customer> customerList = customerService.findAllCustomer(customer);
        // 得到分页相关信息
        PageInfo pageInfo = new PageInfo(customerList);

        TableData tableData = new TableData();

        tableData.setCode(0);
        tableData.setMsg("success");
        tableData.setCount((int) pageInfo.getTotal());
        tableData.setData(customerList);

        return tableData;

    }
    @RequestMapping("/tree")
    public List<Customer> findCustomer(Customer customer){

        List<Customer> customerList = customerService.findAllCustomer(customer);

        return customerList;

    }
    @RequestMapping("/add")
    public AjaxMessage add(Customer customer){
        try {
            customerService.addCustomer(customer);
            return new AjaxMessage(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(false, "添加失败");
        }
    }
    @RequestMapping("/update")
    public AjaxMessage update(Customer customer){
        try {
            customerService.updateCustomer(customer);
            return new AjaxMessage(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(false, "修改失败");
        }
    }
    @RequestMapping("/del")
    public AjaxMessage delete(Integer[] ids){
        try {
            customerService.deleteCustomer(ids);
            return new AjaxMessage(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(false, "删除失败");
        }
    }


}
